package com.kaifamiao.demo.d0824;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

public class Demo09 {

    public static void main(String[] args) {
        /**
         * 将Date对象转换成LocalDate/LocalTime/LocalDateTime
         *    1. 将java.util.Date对象所表示的瞬间转换成java.time.Instant
         *    2. 通过java.time.Instant获取具有时区的日期时间java.time.ZonedDateTime
         *    3. 将java.time.ZonedDateTime转换成相应类型
         */
        Date date = new Date(); // 时刻
        System.out.println(date);
        Instant instant = date.toInstant(); // 瞬间

//        ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault()); // 加上时区信息
        ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of("Europe/Paris"));
        LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();

        System.out.println(localDateTime);


        /**
         * 将LocalDateTime对象转换成Date对象
         * 	 1. 获取带时区的java.time.ZonedDateTime对象
         *   2. 将java.time.ZonedDateTime对象转换成表示某一瞬间的java.time.Instant对象
         *   3. 通过Date.from方法，将java.time.Instant对象转换成Date对象
         */

        LocalDateTime localDateTime1 = LocalDateTime.now();

        ZonedDateTime zonedDateTime1 = localDateTime1.atZone(ZoneId.of("Europe/Paris"));
        Instant instant1 = zonedDateTime1.toInstant();

        Date from = Date.from(instant1);
        System.out.println(from);
    }
}
